import fs from 'node:fs';
import download from 'download-git-repo';
import ora from 'ora';

import path from 'node:path';
import fsExtra from 'fs-extra';

import Handlebars from 'handlebars';
import ejs from 'ejs';
import _ from 'lodash-es';
const spinner = ora('下载中...');

console.log(ejs, 'aaaaaaaa');

//验证路径
export const checkPath = (path) => {
    return fs.existsSync(path);
};

// 帕斯卡命名式，也叫大驼峰命名
export function pascalCase(str) {
    return _.upperFirst(_.camelCase(str));
}

//下载
export const downloadTemp = (branch, project) => {
    spinner.start();
    return new Promise((resolve, reject) => {
        download(`direct:https://gitee.com/liweibty/pdv-app-template.git`, project, { clone: true }, function (err) {
            if (err) {
                reject(err);
                console.log(err);
            }
            resolve();
            spinner.succeed('下载完成');
        });
    });
};
// 编译模板（同步）
export const compileTplSync = (tplPath, targetPath, context) => {
    // 递归文件夹
    function loopGenerate(curPath, curTargetPath) {
        fsExtra.mkdirSync(curTargetPath);
        const dirs = fs.readdirSync(curPath);
        dirs.forEach((dir) => {
            const tplFile = path.resolve(`${curPath}/${dir}`);
            const stat = fs.lstatSync(tplFile);

            if (stat.isDirectory()) {
                // 文件夹
                loopGenerate(tplFile, path.resolve(`${curTargetPath}/${dir}`));
            } else {
                // 文件
                if (path.extname(dir) === '.hbs') {
                    // .hbs模板生成
                    // 获取模板内容
                    const targetFileName = path.parse(dir).name;
                    const targetFile = path.resolve(curTargetPath, targetFileName);
                    const tplContent = fs.readFileSync(tplFile, 'utf-8');
                    // 模板编译
                    Handlebars.registerHelper('pascalCase', pascalCase);
                    Handlebars.registerHelper('camelCase', _.camelCase);
                    Handlebars.registerHelper('kebabCase', _.kebabCase);

                    const template = Handlebars.compile(tplContent);

                    const targetContent = template(context);
                    // 写入编译内容
                    fsExtra.writeFileSync(targetFile, targetContent);
                } else if (path.extname(dir) === '.ejs') {
                    // .ejs模板生成
                    // 获取模板内容
                    const targetFileName = path.parse(dir).name;
                    const targetFile = path.resolve(curTargetPath, targetFileName);
                    const tplContent = fs.readFileSync(tplFile, 'utf-8');
                    const helpers = {
                        pascalCase,
                        camelCase: _.camelCase,
                        kebabCase: _.kebabCase,
                    };
                    // 模板编译
                    const targetContent = ejs.render(tplContent, {
                        ...context,
                        ...helpers,
                    });
                    // 写入编译内容
                    fsExtra.writeFileSync(targetFile, targetContent);
                } else {
                    // 复制文件
                    fsExtra.copySync(tplFile, `${curTargetPath}/${dir}`);
                }
            }
        });
    }
    loopGenerate(tplPath, targetPath);
};
